package com.beifu.manager.service.assetinfo.impl;


import java.util.List;

import com.beifu.manager.api.domain.warehouse.RfidWithTypeId;
import com.beifu.manager.api.domain.wash.RfIdOnly;
import com.beifu.manager.dao.AssetBaseMapper;
import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.model.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beifu.manager.dao.AssetInfoMapper;
import com.beifu.manager.service.assetinfo.AssetInfoService;
import com.utils.PageUtils;

/**
 * 
* Title: AssetInfoServiceImpl
* Description:
* @author    BaiBing
* @date      2018年6月10日
 */
@Service
public class AssetInfoServiceImpl implements AssetInfoService {

	private Logger logger = LoggerFactory.getLogger(AssetInfoServiceImpl.class);

	  @Autowired
	  private AssetInfoMapper assetInfoMapper;
	  @Autowired
	  private AssetTypeMapper assetTypeMapper;
	  @Autowired
	  private AssetBaseMapper assetBaseMapper;

	  @Override
	    public PageUtils getAssetInfoPage(PageUtils page){
	    	 AssetInfoExample example = (AssetInfoExample) page.getQueryParames();
	    	 List<AssetInfo> list = assetInfoMapper.getAssetInfoPage((page.getPage()-1)*page.getLimit(),page.getLimit(), example);
	    	 int count = assetInfoMapper.countByExample(example);
	         page.setList(list);
	         page.setCountNum(count);
	         return page;
		}

	@Override
	public void saveBaseRfidList(JSONObject jsonIn) {
		if (!JSONUtils.isNull(jsonIn)) {
			StringUtils.isNotBlank(jsonIn.get("baseRfidList").toString());
		}
		String baseRfidList = jsonIn.get("baseRfidList").toString();
		JSONArray json = JSONArray.fromObject(baseRfidList);
		List<RfidWithTypeId> rfidWithTypeIdList= (List<RfidWithTypeId>) JSONArray.toCollection(json, RfidWithTypeId.class);

		for(RfidWithTypeId rfidWithTypeId :rfidWithTypeIdList){
			String rfid = rfidWithTypeId.getRFID();
			AssetInfoExample assetInfoExample = new AssetInfoExample();
			assetInfoExample.createCriteria().andRfidCodeEqualTo(rfid);
			List<AssetInfo> assetInfoList = assetInfoMapper.selectByExample(assetInfoExample);
			if(assetInfoList!=null && assetInfoList.size()>0){
//				throw  new RuntimeException("已经有改rfid:"+rfid);
				logger.error("已经有改rfid:"+rfid);
				continue;
			}

			String typeid = rfidWithTypeId.getTypeid();
			AssetType assetType = assetTypeMapper.selectByPrimaryKey(Integer.valueOf(typeid));
			if(null == assetType){
				throw new RuntimeException("无对应的类型！");
			}
			AssetBaseExample assetBaseExample = new AssetBaseExample();
			assetBaseExample.createCriteria().andAssetTypeIdEqualTo(Integer.valueOf(typeid));
			List<AssetBase> assetBaseList = assetBaseMapper.selectByExample(assetBaseExample);
			Integer id = null;
			if(assetBaseList!=null && assetBaseList.size()>0){
				AssetBase assetBase = assetBaseList.get(0);
				id = assetBase.getId();
			}else{
				throw new RuntimeException("不存在对应的baseId");
			}

			AssetInfo assetInfo = new AssetInfo();
			assetInfo.setAssetBaseid(id);
			assetInfo.setRfidCode(rfid);
			assetInfo.setVenderId(1);
			assetInfo.setCurrentOrganizerId(45);
			assetInfo.setStatus(1);
			assetInfo.setCurrentPostion("北京大胡同");
			assetInfo.setWashNum(0);
			assetInfo.setIsScrap("0");
			int i = assetInfoMapper.insertSelective(assetInfo);
		}

	}


}
